﻿using Hangfire;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TodoApp.GoodsModel;
using Volo.Abp.Domain.Repositories;

namespace TodoApp
{
    /// <summary>
    ///  Hangfire 接口 
    /// </summary>
    public class HangFireService : BackgroundService
    {

        private readonly IRepository<TodoItem, Guid> _todoItemRepository;

        public HangFireService(IRepository<TodoItem, Guid> todoItemRepository)
        {
            _todoItemRepository = todoItemRepository;
        }

        /// <summary>
        ///  实现抽象类 
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            // 每分钟呢 执行 一次 
            RecurringJob.AddOrUpdate( () => HangfireJob() , Cron.Minutely() );
            return Task.CompletedTask;
        }


        public void HangfireJob() {

            var str = new TodoItem() { Text ="开心每一天" };
            _todoItemRepository.InsertAsync(str);
           
        }
    }
}
